<?
//baca_permisi(63);
//otoritas(34);

function lihat(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cakses;
	$txtout.="<form name=form_cr method=post action=\"?\">
		<table border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"hitam\">
		<tr>
		<td>Search :&nbsp;</td>
		<td>
		<input name=\"inp_par[srcnama]\" type=\"text\" class=\"form1\" size=\"30\" value=\"$inp_par[srcnama]\">
		</td>
		<td>&nbsp;<input type=submit name=\"go\" value=\"GO &raquo;\" class=\"tombolgo\"></td>
		</tr>
		</table>
		</form>";

        $kata=strtolower(trim($inp_par[srcnama]));
	if($kata<>""){
		$sql_tambahan .= " where (lower(nama_peran) like('%".$kata."%'))";
	}
        $sql="select * from tbl_peran $sql_tambahan order by id_peran asc";
        $arr_tabel=definisi_halaman($sql);
	$result1=$arr_tabel["res_sql"];
	$i=1+$inp_par[awalan]*$inp_par[jumlah_per_hal];

        $txtout.="<form name=\"form2\" method=\"post\">
		<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#C6E6FF\" class=\"tabel1\">
		<tr class=\"tbltagihan\">
		<td width=\"60\"><div align=\"center\">NO</div></td>
		<td><div align=\"center\">NAMA HAK AKSES</div></td>
		<td width=\"60\"><div align=\"center\">Aksi</td>
		</tr>";
        $no=0;
        while($r1=pg_fetch_array($result1)){
                $no++;
		if($warna=="#E3F1FC"){
			$warna="#F4FAFF";
		}else{
			$warna="#E3F1FC";
		}
		$txtout.="
			<tr onmouseover=\"this.style.backgroundColor='#FDFFDD';\" style=\"background-color: $warna;\" onmouseout=\"this.style.backgroundColor='$warna';\">
			<td>$no</td>
			<td>$r1[nama_peran]</td>
			<td width=60><div align=\"center\">";

		if($permisi["edit"]=="izin"){
			$txtout.="<a href=\"?inp_par[mode]=edit&inp_par[id_peran]=$r1[id_peran]".var_kirim($inp_par,"id_peran")."\" title=\"Ubah Data\" class=\"edit\">Edit</a>";
		}
		if($permisi["delete"]=="izin"){
			$txtout.=" | <a href=\"?inp_par[mode]=del&inp_par[id_peran]=$r1[id_peran]".var_kirim($inp_par,"id_peran")."\" onclick=\"return confirm('Anda Yakin Akan Menghapus Data Ini??')\"  title=\"Hapus Data\" class=\"delete\">Delete</a>";
		}

		$txtout.=" </div></td></tr>";
		$i++;
	}

        $txtout.="</table>
		<table width=\"98%\" border=\"0\" align=\"center\" cellpadding=\"0\" cellspacing=\"0\" class=\"tbltagihan\">
		<tr>
		<td>&nbsp;";
	if($permisi["add"]=="izin"){
		$txtout.=" <input type=\"button\" name=\"Submit3\" value=\"Tambah Data &raquo;\" class=\"tomboladd\" onclick='javascript:window.location=\"?inp_par[mode]=add&inp_par[cnospk]=$cnospk_pilih".var_kirim($inp_par,"mode")."\";'>";
	}

	$txtout.="</td>
		<td>$arr_tabel[tabel_halaman]</td>
		</tr>
		</table>
                </form>";
	//End Tampilan Data//

	return $txtout;
}

function tambah(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cnospk_pilih;
        $sql= "select max(id_peran) as nilai from tbl_peran order by nilai desc limit 1";
        $tmpid=cari_nilai($sql)+1;
        $sql = "insert into tbl_peran values ($tmpid, '$inp[txtnama]')";
        dbresult($sql);
        update_menu_peran($tmpid);
}

function ubah(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cnospk_pilih;
        $sql = "update tbl_peran set nama_peran='$inp[txtnama]' where id_peran='$inp_par[id_peran]'";
        dbresult($sql);
        update_menu_peran($inp_par[id_peran]);
}

function update_menu_peran($id_peran){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cnospk_pilih;

        $sql="select * from tbl_feature";
        $r_1=dbresult($sql);
        while($data_1=pg_fetch_array($r_1)){
                $sql="select * from tbl_sub_feature where feature_id='$data_1[feature_id]'";
                $r_2=dbresult($sql);
                while($data_2=pg_fetch_array($r_2)){
                        if( $inp["cmb_".$data_1[feature_id]."_".$data_2[sub_f_id]]!="" ){
                                $arr_sql[]="insert into tbl_menu_peran values ('$id_peran', '$data_2[sub_f_id]', '".$inp["cmb_".$data_1[feature_id]."_".$data_2[sub_f_id]]."')";
                        }
                }
        }
        if( count($arr_sql)>0 ){
                $sql="delete from tbl_menu_peran where id_peran='$id_peran'";
                dbresult($sql);
                $sql=implode("; ", $arr_sql);
                dbresult($sql);
        }
}
function hapus(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cnospk_pilih;
        $sql = "delete from tbl_peran where id_peran='$inp_par[id_peran]'";
        dbresult($sql);
        $sql="delete from tbl_menu_peran where id_peran='$inp_par[id_peran]'";
        dbresult($sql);
}

function form(){
        global $inp_par,$inp,$permisi,$sql_tambahan,$cnospk_pilih;
        global $userfile,$cnospk_pilih,$foto_no;
        global $userfile_name,$tgl_foto_proyek;
        global $id_peran, $level, $cakses;
        generate_rule("is_isi","inp[txtnama]","Anda Harus Mengisi Nama Peran");
        $txtout.=generate_validasi();
        if($inp_par[mode]=="edit"){
                $sql="select * from tbl_peran where id_peran='$inp_par[id_peran]'";
//				echo $sql."<br>";
                $result = dbresult($sql);
                $r = pg_fetch_array($result);
                $arr_nilai=tabel_array("select sub_f_id as user_id,menu_access as level_access from tbl_menu_peran where id_peran='$inp_par[id_peran]'");
        }
        
        $txtout.="<form name=\"form1\" method=\"post\" action=\"?".var_kirim($inp_par)."\" enctype=\"multipart/form-data\" onsubmit=\"return validasi(document.form);\">
                <table width=\"98%\" align=\"center\" border=\"0\" cellpadding=\"3\" cellspacing=\"1\" bgcolor=\"#C6E6FF\">
                <tr bgcolor=\"#0066CC\">
                    <td height=20 colspan=3>
                        <font color=#FFFFFF size=1 face=\"Verdana, Arial, Helvetica, sans-serif\"><strong>&nbsp;FORM INPUT PERAN</strong></font>
                    </td>
                </tr>
                <tr bgcolor=\"#FFFFFF\">
                        <td width=\"100\"><font size=\"2\"><strong>&nbsp;Nama Peran</strong></font></td>
                        <td><input type=\"text\" class=\"form2\" name=\"inp[txtnama]\" value=\"$r[nama_peran]\"></td>
                </tr>
                <tr bgcolor=\"#0066CC\">
                        <td align=center colspan=3>&nbsp;</td>
                </tr>
                <tr bgcolor=\"#FEFBE9\">
                        <td colspan=\"3\">


<table width=\"100%\" border=\"0\" cellpadding=\"2\" cellspacing=\"1\" bgcolor=\"#E3F1FC\">
  <tr bgcolor=\"#FDE4A4\">
        <td width=\"100%\"><div align=\"center\"><font size=\"2\"><strong>DETAIL FITUR</strong></font></div></td>
  </tr>
  <tr>
    <td><table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"2\">";
        $hal=6;
        $sql="select * from tbl_feature";
		//echo $sql;
        $res=dbresult($sql);
        while($r=pg_fetch_array($res)){
        $i+=1;
        $txtout.="
        <tr bgcolor=#cccccc>
          <td width=\"24%\" colspan=$hal><font size=\"2\"><strong>$i. $r[feature_name]&nbsp;</strong></font>";

        if($cakses==6){
                $out.=combo_array("cmb_$r[feature_id]",$level,"");
                $out.="<input type=\"checkbox\" name=\"checkbox_$r[feature_id]\" value=\"checkbox\"  onClick=\"if (this.checked) {convertcombo('cmb_$r[feature_id]','inp[cmb_$r[feature_id]_')}\">";
        }else if($cakses==5 and $lev[level_akses]<>5 and $lev[level_akses]<>6){
                $out.=combo_array("cmb_$r[feature_id]",$level1,"");
                $out.="<input type=\"checkbox\" name=\"checkbox_$r[feature_id]\" value=\"checkbox\"  onClick=\"if (this.checked) {convertcombo('cmb_$r[feature_id]','inp[cmb_$r[feature_id]_')}\">";
        }else if($cakses==5 and $lev[level_akses]==5){
                $out.="<font size=2><b>Anda Bukan Super Admin</b></font>";
        }else{
                $out.="<font size=2><b>Anda Bukan Super Admin</b></font>";
        }
        $txtout.=$out;
        $txtout.="
            <font size=\"2\"><em>(Semua fitur PAJAK) </em></font></td>
        </tr>
        ";
        $hal=3;
        $sql_sub="select * from tbl_sub_feature where feature_id=$r[feature_id] order by sub_f_id";
		//echo $sql_sub." ORA<br>";
        $res_sub=dbresult($sql_sub);
        $jml_rec=pg_num_rows($res_sub);
        $jml_hal=$jml_rec/$hal;
		for($h=0;$h<$jml_hal;$h++){
                $txtout.="<tr>";
                for($j=0;$j<$hal;$j++){
                        $curr_pos=$h*$hal+$j;
                        $curr_pos=$curr_pos;
                        if($curr_pos<$jml_rec){
				$r_sub=pg_fetch_array($res_sub);
                                $nama="inp[cmb_$r[feature_id]_$r_sub[sub_f_id]]";
                                if($cakses==6){
                                        $kata=combo_array2($nama,$level,$arr_nilai[$r_sub[sub_f_id]])." ".$r_sub["sub_f_name"];
                                }else if($cakses==5 and $lev[level_akses]<>5 and $lev[level_akses]<>6){
                                        $kata=combo_array2($nama,$level1,$arr_nilai[$r_sub[sub_f_id]])." ".$r_sub["sub_f_name"];
                                }else if($cakses==5 and $lev[level_akses]==5){
                                        $kata.="";
                                }else{
                                        $kata.="";
                                }
                        }else{
                                $kata="";
                        }
                        $txtout.="<td><font face=arial size=1>&nbsp;$kata</font></td>";
                }
                $txtout.="</tr>";
        }
        }
        
$txtout.="
        </table>
        </td></tr>
        </td>
        </tr>
        </td>
        </tr>
        </table>
        </td>
        </tr>
                <tr bgcolor=#1A75BF>
                        <td align=center colspan=3>
                                <input type=submit value=\"Save &raquo;\" class=\"tomboladd\" name=\"inp[aksi]\">
                                <input type=button value=\"Cancel &raquo;\" onclick='javascript:history.go(-1)' class=\"tomboladd\">
                        </td>
                </tr>
                </table>
        </form>";

        return $txtout;
}
function generate_form($inp_par){
        global $inp_par,$inp;
        switch ($inp_par[mode]) {
                case "add":
                        if($inp[aksi]==""){
                                $txtout.=form();
                        }else{
                                $txtout.=tambah();
                                $txtout.=lihat();

                        }
                break;
                case "edit":
                        if($inp[aksi]==""){
                                $txtout.=form();
                        }else{
                                $txtout.=ubah();
                                $txtout.=lihat();

                        }
                break;
                case "del":
                        $txtout.=hapus();
                $txtout.=lihat();
                break;
                default:
                $txtout.=lihat();
                break;
        }
        return $txtout;
}
?>
